# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import cv2 as cv
import numpy as np

pic1 = "E:/Study/CSDN人工智能/2-week-vision/作业/book/0.jpg"
pic2 = "E:/Study/CSDN人工智能/2-week-vision/作业/book/1.jpg"
# pic = "E:/Study/CSDN人工智能/secondweek/road.png"
image1 = cv.imdecode(np.fromfile(pic1,dtype=np.uint8),-1)
image2 = cv.imdecode(np.fromfile(pic2,dtype=np.uint8),-1)
image1 = cv.resize(image1, (0, 0), fx=0.15, fy=0.15)
image2 = cv.resize(image2, (0, 0), fx=0.15, fy=0.15)
print(np.shape(image1))
gray1 = cv.cvtColor(image1, cv.COLOR_BGR2GRAY)
gray2 = cv.cvtColor(image2, cv.COLOR_BGR2GRAY)

orb = cv.ORB_create(200)

kp1, des1 = orb.detectAndCompute(gray1,None)
kp2, des2 = orb.detectAndCompute(gray2, None)
matcher=cv.DescriptorMatcher_create("BruteForce")
matches_ob = matcher.match(des1, des2)

good = []
distance = []
for m in matches_ob:
    distance.append(m.distance)
max_dist=max(distance)

for m in matches_ob:
    if m.distance < 0.6 * max_dist:
        good.append(m)


# print(matches_ob[0].distance)
img_orb = cv.drawMatches(image1, kp1, image2, kp2, matches_ob,None)
img_good = cv.drawMatches(image1, kp1, image2, kp2, good,None)

cv.imshow("good", img_good)
cv.imshow("ORB", img_orb)

cv.waitKey(0)
